package com.larissa.android.quiz;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.larissa.android.quiz.databinding.ActivityHelpBinding;

public class HelpActivity extends AppCompatActivity {
    public static final String RESULT_KEY="result";
    private static final String KEY="help";
    private boolean answer;
    private TextView textView;
    private Button btnCheck;
    private boolean hasHelped=false;
    private ActivityHelpBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityHelpBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        answer=getIntent().getBooleanExtra(MainActivity.ANSWER_KEY,true);
        textView=binding.tvAnswer;
        btnCheck=binding.btnCheckAnswer;

        btnCheck.setOnClickListener(v->updateAnswer());

        if(savedInstanceState!=null){
            hasHelped=savedInstanceState.getBoolean(KEY);
            if(hasHelped) updateAnswer();
        }
    }

    private void updateAnswer(){
        String msg=String.format("The answer is %s",answer);
        textView.setText(msg);
        btnCheck.setEnabled(false);
        hasHelped=true;
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(KEY,hasHelped);
    }

    @Override
    public void finish() {
        Intent intent=new Intent();
        intent.putExtra(RESULT_KEY,hasHelped);
        setResult(RESULT_OK,intent);
        super.finish();
    }
}